﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public class Tower : PuzzleObject
{
    private List<Direction> shotDirection;
    public List<Direction> ShotDirection
    {
        get { return shotDirection; }
    }

    private List<Shot> shotList;
    public List<Shot> ShotList
    {
        get { return shotList; }
    }

    public double lastShot;

    public Tower(ObjectKind kind, int xposition, int yposition)
        :base(kind, xposition,yposition, false)
    {
        shotList = new List<Shot>();
        shotDirection = new List<Direction>();

        lastShot = 0;
    }

    public void Update(GameTime gameTime)
    {
        if (lastShot == 0)
            lastShot = gameTime.TotalGameTime.TotalMilliseconds;
        
        //Console.WriteLine(lastShot + " " + gameTime.TotalRealTime.TotalMilliseconds);

        if (lastShot + 2000 <= gameTime.TotalGameTime.TotalMilliseconds)
        {
            lastShot = gameTime.TotalGameTime.TotalMilliseconds;

            foreach (Direction dir in shotDirection)
            {
                Shot shot= null;
                if(dir == Direction.Left || dir == Direction.Right)
                    shot = new Shot((int)xposition * 64 + 26, (int)yposition * 64 + 40, dir, this);
                else
                    shot = new Shot((int)xposition * 64 + 30, (int)yposition * 64 + 10, dir, this);

                shotList.Add(shot);
                shot.sprite.PlayAnimation(shot.shotAnimation);
            }
            
        }

        List<Shot> shotToRemove = new List<Shot>();

        foreach (Shot shot in shotList)
        {
            if (!shot.Update())
            {
                shotToRemove.Add(shot);
            }
        }
        foreach (Shot shot in shotToRemove)
        {
            if (!shot.Alive)
            {
                shotList.Remove(shot);
            }
        }

    }

}
